from django.db.models import Q
from flask import request
from rest_framework import serializers
from .models import Station, JianLi, ResumeStatus,Engineer,Interviewer,StationLog,Manage


# 岗位序列化器
class StationSerializer(serializers.ModelSerializer):
    jianli_num = serializers.SerializerMethodField()
    employed_num = serializers.SerializerMethodField()
    adopt_num = serializers.SerializerMethodField()
    stay_num = serializers.SerializerMethodField()
    opening_time = serializers.SerializerMethodField()
    closing_time = serializers.SerializerMethodField()
    interview_name_list = serializers.SerializerMethodField()
    manage_name_list = serializers.SerializerMethodField()


    class Meta:
        model = Station
        fields = '__all__'

    def get_jianli_num(self,obj):
        num = ResumeStatus.objects.filter(Q(jianli__position=obj) & ~Q(interview_progress='待推送'))
        if request.args.get('PASS_USER',''):
            engineer = Engineer.objects.filter(hi_id=request.args.get('PASS_USER','')).first()
            if engineer:
                num = num.filter(jianli__recommend_company=engineer.group)
            else:
                return 0
        return num.count()

    # 已入职
    def get_employed_num(self,obj):
        num = ResumeStatus.objects.filter(jianli__position=obj, joined_or_not='是')
        if request.args.get('PASS_USER',''):
            engineer = Engineer.objects.filter(hi_id=request.args.get('PASS_USER','')).first()
            if engineer:
                num = num.filter(jianli__recommend_company=engineer.group)
            else:
                return 0
        return num.count()

    # 通过
    def get_adopt_num(self,obj):
        num = ResumeStatus.objects.filter(jianli__position=obj, interview_result='面试通过')
        if request.args.get('PASS_USER',''):
            engineer = Engineer.objects.filter(hi_id=request.args.get('PASS_USER','')).first()
            if engineer:
                num = num.filter(jianli__recommend_company=engineer.group)
            else:
                return 0
        return num.count()
    def get_stay_num(self,obj):
        num = ResumeStatus.objects.filter(jianli__position=obj, interview_progress='待推送')
        if request.args.get('PASS_USER',''):
            engineer = Engineer.objects.filter(hi_id=request.args.get('PASS_USER','')).first()
            if engineer:
                num = num.filter(jianli__recommend_company=engineer.group)
            else:
                return 0
        return num.count()

    def get_opening_time(self, obj):
        if obj.opening_time:
            return obj.opening_time.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None

    def get_closing_time(self, obj):
        if obj.closing_time:
            return obj.closing_time.strftime("%Y-%m-%d %H:%M:%S")
        return None

    def get_interview_name_list(self, obj):
        interviewers = obj.interviewers.all()
        return ','.join([interviewer.name for interviewer in interviewers])

    def get_manage_name_list(self, obj):
        manages = obj.manages.all()
        return ','.join([manages.name for manages in manages])

class JianLiSerializer(serializers.ModelSerializer):
    # resume = serializers.SerializerMethodField()
    recommended_date =serializers.SerializerMethodField()
    class Meta:
        model = JianLi
        fields = '__all__'

    def get_recommended_date(self, obj):
        if obj.recommended_date:
            return obj.recommended_date.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None

class ResumeStatusSerializer(serializers.ModelSerializer):
    entry_time = serializers.SerializerMethodField()
    create_time = serializers.SerializerMethodField()
    interview_time= serializers.SerializerMethodField()
    class Meta:
        model = ResumeStatus
        fields = '__all__'

    def get_entry_time(self, obj):
        if obj.entry_time:
            return obj.entry_time.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None

    def get_create_time(self, obj):
        if obj.create_time:
            return obj.create_time.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None
    def get_interview_time(self, obj):
        if obj.interview_time:
            return obj.interview_time.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None
class JianliXxSerializer(JianLiSerializer):
    position_name = serializers.SerializerMethodField()
    slt_position = serializers.SerializerMethodField()
    status = ResumeStatusSerializer(source='resume_status')

    def get_position_name(self,obj):
        return obj.position.position

    def get_slt_position(self,obj):
        return obj.position.slt_position
class EngineerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Engineer
        fields = '__all__'

class InterviewerSerializer(serializers.ModelSerializer):
    position = serializers.SerializerMethodField()
    class Meta:
        model = Interviewer
        fields = '__all__'

    def get_position(self,obj):
        positions = obj.position.all()
        return [station.position for station in positions]

class ManagesSerializer(serializers.ModelSerializer):
    position = serializers.SerializerMethodField()
    class Meta:
        model = Manage
        fields = '__all__'

    def get_position(self,obj):
        positions = obj.position.all()
        return [station.position for station in positions]
class StationBasicSerializer(serializers.ModelSerializer):
    interview_name_list = serializers.SerializerMethodField()
    manage_name_list = serializers.SerializerMethodField()

    class Meta:
        model = Station
        fields = ['position','hcnumber','base','level','position_state','position_jd','service','priority','slt_position','manage_name_list','interview_name_list']

    def get_interview_name_list(self, obj):
        interviewers = obj.interviewers.all()
        return ','.join([interviewer.name for interviewer in interviewers])

    def get_manage_name_list(self, obj):
        manages = obj.manages.all()
        return ','.join([manages.name for manages in manages])

class StationLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = StationLog
        fields = '__all__'

